//
//  CZNewFeatureCell.swift
//  weibo10
//
//  Created by apple on 16/5/14.
//  Copyright © 2016年 apple. All rights reserved.
/*
1 懒加载背景view

*/

import UIKit

class CZNewFeatureCell: UICollectionViewCell {
    
    //根据cell显示页面设置对应的图片
    var index: Int = 0
        {
        
        didSet
        {
        bkgImageView.image = UIImage(named: "new_feature_\(index + 1)")
        //隐藏按钮
        startButton.hidden = true
        }
    
    }
    
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        prepareUI()
        
       
    }

    
    
    
    
    
    private func prepareUI()
    {
    contentView.addSubview(bkgImageView)
    contentView.addSubview(startButton)
        
       //添加约束
       bkgImageView.snp_makeConstraints { (make) -> Void in
        make.edges.equalTo(self.contentView)
        
        }
        
        startButton.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(contentView)
            make.bottom.equalTo(contentView).offset(-160)
        }
        
        
    }
    
    
    //开始按钮动画
    func startAnimation()
    {
        startButton.hidden = false
        
    //在动画前设置transform.scale为0 看不到
        startButton.transform = CGAffineTransformMakeScale(0, 0)
        
   UIView.animateWithDuration(1, delay: 0.5, usingSpringWithDamping: 0.5, initialSpringVelocity: 5, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
    
    //恢复到原来大小
    self.startButton.transform = CGAffineTransformMakeScale(1, 1)
    
    

    }, completion: nil)
    
    }
    
    
    //MARK: - 添加按钮事件
    func startButtonDidClick()
    {
    //切换到CZMainViewController
        let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
        appDelegate.switchRootViewController(CZMainTabBarController())
    
    }
    
              
    
  //MARK: - 懒加载
    private lazy var bkgImageView: UIImageView = UIImageView(image: UIImage(named: "new_feature_1"))
    
    //开始按钮
    private lazy var startButton: UIButton =
    {
        
    let button = UIButton()
        
        //设置文字
        button.setTitle("开始体验", forState: UIControlState.Normal)
        
        button.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal)
        
        button.setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "new_feature_finish_button_highlighted"), forState: UIControlState.Highlighted)
        button.hidden = true
        
        //点击事件
        button.addTarget(self, action: "startButtonDidClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        return button
    
    }()
    
    
    
    
    
}
